//	Dragons & Warriors : A card-driven wargame
//	Copyright(C) 2022 Li Zhengyao

//	This program is free software : you can redistribute it and /or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.

//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
//	GNU General Public License for more details.

//	You should have received a copy of the GNU General Public License
//	along with this program.If not, see < https://www.gnu.org/licenses/>.

#include <thread>

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_image.h"

#include "DataDefine.h"
#include "globalVariables.h"


extern int globalState;

int initialize();
int destruct();
int GUIMapping();
int GUILeftButtonDown();
int GUILeftButtonUp();
int gameplayLeftButtonDown();
int gameplayLeftButtonUp();
int netprocessLeftButtonDown();
int netprocessLeftButtonUp();
int deckeditLeftButtonDown();
int deckeditLeftButtonUp();
int dataProcess();
int mainRender(SDL_Renderer* renderer);

void udpWaiting();
void tcpWaiting();
void tcpShakingHands();

int main(int argc, char** argv)
{
	bool quit = false;
	SDL_Event event;

	//initialization
	SDL_Init(SDL_INIT_VIDEO);
	IMG_Init(IMG_INIT_PNG);
	TTF_Init(); 

	window = SDL_CreateWindow("SDLTest",
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);

	renderer = SDL_CreateRenderer(window, -1, 0);

	SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);

	//everything initialization
	initialize();

	thread udpReceiverThread(udpWaiting);
	thread tcpReceiverThread(tcpWaiting);
	thread tcpSHThread(tcpShakingHands);

	//gui design
	GUIMapping();

	//re-focus
	SDL_SetWindowInputFocus(window);

	while (!quit)
	{
		//frame rate controll
		SDL_Delay(1000/FPS);

		SDL_PollEvent(&event);

		//main cycle
		//data process
		dataProcess();
		//rendering
		mainRender(renderer);
		//input process
		switch (event.type)
		{
		case SDL_MOUSEBUTTONDOWN:
			mouseX = event.motion.x;
			mouseY = event.motion.y;
			switch (event.button.button)
			{
			case SDL_BUTTON_LEFT:
				GUILeftButtonDown();
				if (globalState == GAMEPLAYING)
				{
					gameplayLeftButtonDown();
				}
				else if (globalState == NETPROCESS)
				{
					netprocessLeftButtonDown();
				}
				else if (globalState == DECKEDIT)
				{
					deckeditLeftButtonDown();
				}
				break;
			}
			break;
		case SDL_MOUSEBUTTONUP:
			mouseX = event.motion.x;
			mouseY = event.motion.y;
			switch (event.button.button)
			{
			case SDL_BUTTON_LEFT:
				GUILeftButtonUp();
				if (globalState == GAMEPLAYING)
				{
					gameplayLeftButtonUp();
				}
				else if (globalState == NETPROCESS)
				{
					netprocessLeftButtonUp();
				}
				else if (globalState == DECKEDIT)
				{
					deckeditLeftButtonUp();
				}
				break;
			}
			break;
		case SDL_WINDOWEVENT:
			switch (event.window.event)
			{
			case SDL_WINDOWEVENT_CLOSE:
				quit = true;
				isQuit = true;
				break;
			}
			break;
		case SDL_QUIT:
			quit = true;
			break;
		}
	}

	//destruct all
	destruct();

	//release resourses
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
	IMG_Quit();
	TTF_Quit();

	udpReceiverThread.join();
	tcpReceiverThread.join();
	tcpSHThread.join();

	return 0;
}